package cn.stylefeng.guns.modular.rest;

import cn.stylefeng.guns.modular.sht.entity.Question;
import cn.stylefeng.guns.modular.sht.entity.StudyHistory;
import cn.stylefeng.guns.modular.sht.model.params.QuestionParam;
import cn.stylefeng.guns.modular.sht.model.params.StudyHistoryParam;
import cn.stylefeng.guns.modular.sht.service.QuestionService;
import cn.stylefeng.guns.modular.sht.service.ShtStudyHistoryService;
import cn.stylefeng.guns.modular.sht.model.result.StudyHistoryResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;


/**
 * 考题信息控制器
 *
 * @author
 * @Date 2021-01-22 20:22:52
 */
@RestController
@RequestMapping("/miniprogramApi/question")
public class ApiQuestionController extends BaseController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private ShtStudyHistoryService studyHistoryService;

    // TODO 增加grouptime参数，不等于空就相当于继续答题。等于空就是从头开始新答题。
    @RequestMapping(value = "random", method = RequestMethod.POST)
    public Object random(int questionBankId, @RequestParam String questionIdList, String openId, String groupTime) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        // String groupTime = UUID.randomUUID().toString().replaceAll("-", "");
        JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, Integer.class);
        List<Integer> list = (List<Integer>) mapper.readValue(questionIdList, jt);

        Map map = new HashMap();
        SUCCESS_TIP.setData(map);

        // 题目总数
        QuestionParam questionParam = new QuestionParam();
        questionParam.setQuestionBankId(questionBankId);
        int count = questionService.cntTotal(questionParam);
        map.put("total", count);

        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<Question>().eq("question_bank_id", questionBankId);
        // 刨除已经答过的题目
        if (StringUtils.isNotEmpty(groupTime)) {
            if (list != null && list.size() > 0) {
                questionQueryWrapper.notIn("id", list);
            }
        } else {
            // 学习历史总览
            StudyHistoryParam param = new StudyHistoryParam();
            param.setGroupTime(groupTime);
            StudyHistoryResult studyHistoryResult = studyHistoryService.findBySpec(param);
            if (studyHistoryResult.getId() == null) {
                StudyHistory studyHistory = new StudyHistory();
                groupTime = UUID.randomUUID().toString().replaceAll("-", "");
                studyHistory.setGroupTime(groupTime);
                studyHistory.setOpenId(openId);
                studyHistory.setCurrect(0);
                studyHistory.setWrong(0);
                studyHistory.setTotal(count);
                studyHistory.setQuestionBankId(questionBankId);
                studyHistoryService.save(studyHistory);
            }
        }

        List<Question> words = questionService.list(questionQueryWrapper);


        // 当前题库剩余题目总数
        int total = words.size();
        // 木有新的题目了
        if (total == 0) {
            return SUCCESS_TIP;
        }

        Random random = new Random();
        int n = random.nextInt(words.size());
        Question question = words.get(n);
        map.put("question", question);// 问题
        map.put("group", groupTime);// 题目总数


        return SUCCESS_TIP;
    }

}


